package com.work.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.work.dao.InformationDao;
import com.work.ncov.*;
import com.work.util.EmailUtil;
import com.work.util.JsoupUtil;
import org.apache.log4j.Logger;

import java.util.List;

/**
 * @ClassName InfoService
 * @Description: 获取丁香园信息service
 * @Author: WangWenpeng
 * @date: 15:16 2020/1/31
 * @Version 1.0
 */
public class InfoService {
    private static final Logger logger = Logger.getLogger(InfoService.class);
    private boolean sendEmailFlag = false;
    private InformationDao informationDao;

    public InfoService() {
        this.informationDao = new InformationDao();
    }

    public void getInfo() {
        //获取HTML数据
        logger.info("获取信息开始");
        JsoupUtil.getPageByJSoup(DxyNcovInfo.URL_dxy);

        //解析数据开始
        //解析时间线新闻信息  解析全国数据统计  解析地区信息
        StringBuffer statisticSB = dealStatisticInfo();
        StringBuffer timelineSB = dealTimeLineInfo();
        StringBuffer areaStatSB = dealAreaStatInfo();

        if (sendEmailFlag) {
            try {
                new EmailUtil().sendEmail("新型冠状病毒肺炎实时动态", statisticSB.append(timelineSB).append(areaStatSB) + "");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        informationDao.destory();
    }

    /**
     * @Description 处理地市统计信息
     * @Author WangWenpeng
     * @Date 16:03 2020/2/1
     * @Param []
     */
    private StringBuffer dealAreaStatInfo() {
        logger.info("省份地市统计信息begin");
        StringBuffer areaStatSB = new StringBuffer();
        areaStatSB.append(">>>>>>>>>>>>>>>>>>>山东省地市信息统计<br/>");

        //DxyNcovInfo.areaStatInformation = JsoupUtil.getInformation(DxyNcovInfo.AREASTAT_INFORMATION_REGEX_TEMPLATE, "ID", DxyNcovInfo.AREASTAT_INFORMATION_ATTRIBUTE);
        DxyNcovInfo.areaStatInformation = JsoupUtil.getJsonScriptContent(DxyNcovInfo.AREASTAT_INFORMATION_ATTRIBUTE, DxyNcovInfo.AREASTAT_INFORMATION_REGEX_TEMPLATE);
        List<AreaStatProvinceInfo> areaStatProvinceInfoList = JSON.parseObject(DxyNcovInfo.areaStatInformation, new TypeReference<List<AreaStatProvinceInfo>>() {
        });
        logger.info(areaStatProvinceInfoList.toString());

        logger.info("省份地市统计信息begin 省级数据存储");
        int delRst = informationDao.deleteProvinceInfo();
        int recRst = informationDao.recordProvinceInfo(areaStatProvinceInfoList);
        logger.info("省份地市统计信息begin 省级数据存储end 删除" + delRst + " 新增" + recRst);

        logger.info("省份地市统计信息begin 市级数据存储");
        int delCity = informationDao.deleteCityInfo();
        logger.info("省份地市统计信息begin 市级数据存储 删除" + delCity);
        for (AreaStatProvinceInfo areaStatProvinceInfo : areaStatProvinceInfoList) {

            if (sendEmailFlag && "山东".equals(areaStatProvinceInfo.getProvinceShortName())) {
                areaStatSB.append("山东数据统计信息  | 确诊病例  | " + areaStatProvinceInfo.getConfirmedCount()).append("<br/>");
                areaStatSB.append("山东数据统计信息  | 疑似病例  | " + areaStatProvinceInfo.getSuspectedCount()).append("<br/>");
                areaStatSB.append("山东数据统计信息  | 死亡人数  | " + areaStatProvinceInfo.getCuredCount()).append("<br/>");
                areaStatSB.append("山东数据统计信息  | 治愈人数  | " + areaStatProvinceInfo.getDeadCount()).append("<br/>");
            }

            if (areaStatProvinceInfo.getCities().size() > 0) {
                for (int i = 0; i < areaStatProvinceInfo.getCities().size(); i++) {
                    areaStatProvinceInfo.getCities().get(i).setProvinceName(areaStatProvinceInfo.getProvinceName());
                    areaStatProvinceInfo.getCities().get(i).setProvinceShortName(areaStatProvinceInfo.getProvinceShortName());

                    if (sendEmailFlag && "山东".equals(areaStatProvinceInfo.getProvinceShortName())) {
                        areaStatSB.append(areaStatProvinceInfo.getCities().get(i).getCityName() + " |  确诊病例 | " + areaStatProvinceInfo.getCities().get(i).getConfirmedCount());
                        areaStatSB.append("  |  疑似病例  | " + areaStatProvinceInfo.getCities().get(i).getSuspectedCount());
                        areaStatSB.append("  |  死亡人数  | " + areaStatProvinceInfo.getCities().get(i).getCuredCount());
                        areaStatSB.append("  |  治愈人数  | " + areaStatProvinceInfo.getCities().get(i).getDeadCount()).append("<br/>");
                    }

                }
                int recCity = informationDao.recordCityInfo(areaStatProvinceInfo.getCities());
                logger.info("省份地市统计信息begin 市级数据存储 " + areaStatProvinceInfo.getProvinceName() + " 新增" + recCity);
            }
        }
        logger.info("省份地市统计信息begin 市级数据存储end");
        logger.info("省份地市统计信息end");
        return areaStatSB;
    }

    /**
     * @Description 全国数据统计信息  留一个历史数据 给以后的数据折线图使用
     * @Author WangWenpeng
     * @Date 14:26 2020/2/1
     * @Param []
     */
    private StringBuffer dealStatisticInfo() {
        logger.info("全国数据统计信息begin");

        StringBuffer statisticSB = new StringBuffer();
        statisticSB.append(">>>>>>>>>>>>>>>>>>>全国数据统计信息<br/>");

        //DxyNcovInfo.statisticInformation = JsoupUtil.getInformation(DxyNcovInfo.STATISTIC_INFORMATION_REGEX_TEMPLATE, "id", DxyNcovInfo.STATISTIC_INFORMATION_ATTRIBUTE);
        DxyNcovInfo.statisticInformation = JsoupUtil.getJsonScriptContent(DxyNcovInfo.STATISTIC_INFORMATION_ATTRIBUTE, DxyNcovInfo.STATISTIC_INFORMATION_REGEX_TEMPLATE);
        logger.info(DxyNcovInfo.statisticInformation);
        StatisticsInfo statisticsInfo = JSON.parseObject(DxyNcovInfo.statisticInformation, StatisticsInfo.class);
        logger.info("全国数据统计信息  " + statisticsInfo.toString());
        logger.info("全国数据统计信息  确诊病例 " + statisticsInfo.getConfirmedCount());
        logger.info("全国数据统计信息  疑似病例 " + statisticsInfo.getSuspectedCount());
        logger.info("全国数据统计信息  重症病例 " + statisticsInfo.getSeriousCount());
        logger.info("全国数据统计信息  死亡人数 " + statisticsInfo.getCuredCount());
        logger.info("全国数据统计信息  治愈人数 " + statisticsInfo.getDeadCount());

        //记录统计历史记录表
        logger.info("全国数据统计信息  历史数据统计");
        int statisticHisCount = informationDao.queryStatisticCountByModTime(statisticsInfo);
        if (statisticHisCount == 0) {
            informationDao.recordStatisticHisInfo(statisticsInfo);
        }

        //全国数据信息统计表
        logger.info("全国数据统计信息  展示数据统计");
        informationDao.deleteStatisticInfo();
        informationDao.recordStatisticInfo(statisticsInfo);

        statisticSB.append("全国数据统计信息 | 确诊病例 | " + statisticsInfo.getConfirmedCount()).append("<br/>");
        statisticSB.append("全国数据统计信息 | 疑似病例 | " + statisticsInfo.getSuspectedCount()).append("<br/>");
        statisticSB.append("全国数据统计信息 | 重症病例 | " + statisticsInfo.getSeriousCount()).append("<br/>");
        statisticSB.append("全国数据统计信息 | 死亡人数 | " + statisticsInfo.getCuredCount()).append("<br/>");
        statisticSB.append("全国数据统计信息 | 治愈人数 | " + statisticsInfo.getDeadCount()).append("<br/>");

        logger.info("全国数据统计信息 end");
        return statisticSB;
    }

    /**
     * @Description 时间线新闻信息
     * @Author WangWenpeng
     * @Date 14:26 2020/2/1
     * @Param []
     */
    private StringBuffer dealTimeLineInfo() {

        StringBuffer timeLineBuffer = new StringBuffer();
        timeLineBuffer.append("<br/>");
        timeLineBuffer.append(">>>>>>>>>>>>>>>>>>>实时播报<br/>");

        DxyNcovInfo.timelineServiceInformation = JsoupUtil.getInformation(DxyNcovInfo.TIME_LINE_REGEX_TEMPLATE, "id", DxyNcovInfo.TIME_LINE_ATTRIBUTE);
        DxyNcovInfo.timelineServiceInformation = JsoupUtil.getJsonScriptContent(DxyNcovInfo.TIME_LINE_ATTRIBUTE, DxyNcovInfo.TIME_LINE_REGEX_TEMPLATE);
        logger.info("时间线新闻信息begin");
        logger.info(DxyNcovInfo.timelineServiceInformation);

        //时间线信息转json  打出来看看是啥  存一下数据库 成功存储就发邮件
        List<TimeLineServiceInfo> timeLineServiceInfoList = JSON.parseObject(DxyNcovInfo.timelineServiceInformation, new TypeReference<List<TimeLineServiceInfo>>() {
        });
        for (TimeLineServiceInfo timeLineServiceInfo : timeLineServiceInfoList) {
            //logger.info(timeLineServiceInfo.toString());
            int rst = informationDao.recordTimeLineInfo(timeLineServiceInfo);
            if (rst == 1) {
                sendEmailFlag = true;
                timeLineBuffer.append(timeLineServiceInfo.getTitle()).append("<br/>");
                timeLineBuffer.append(timeLineServiceInfo.getSummary()).append("<br/>").append("<br/>");
            }
        }
        logger.info("时间线新闻信息end");
        return timeLineBuffer;
    }

}
